package com.ruoyi.hospital.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.hospital.entity.*;
import com.ruoyi.hospital.entity.order.*;
import com.ruoyi.hospital.entity.schedule.ItemScheduleInfo;
import com.ruoyi.hospital.entity.schedule.ScheduleVO;
import com.ruoyi.service.domain.*;
import com.ruoyi.service.mapper.*;
import com.ruoyi.service.service.IServiceAppinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component
public class HospitalOrderService {

    @Autowired
    private ServiceOrdersMapper ordersMapper;

    @Autowired
    private ServiceNurseMapper nurseMapper;

    @Autowired
    private ServiceNurseitemsMapper nurseitemsMapper;

    @Autowired
    private ServiceMycartMapper mycartMapper;

    @Autowired
    private ServiceItemscomboMapper itemscomboMapper;

    @Autowired
    private ServiceComboMapper comboMapper;

    @Autowired
    private ServiceItemsMapper itemsMapper;

    @Autowired
    private ServiceAddressMapper addressMapper;

    @Autowired
    private ServicePayroadMapper payroadMapper;

    @Autowired
    private ServiceOrderscomboMapper orderscomboMapper;

    @Autowired
    private ServiceOrdersitemsMapper ordersitemsMapper;

    @Autowired
    private ServicePackagescomboMapper packagescomboMapper;

    @Autowired
    private ServicePackagesMapper packagesMapper;

    @Autowired
    private ServiceOrderspackagesMapper orderspackagesMapper;

    @Autowired
    private ServicePayMapper payMapper;

    @Autowired
    private ServiceAssessmentMapper assessmentMapper;

    @Autowired
    private ServicePictureMapper pictureMapper;

    @Autowired
    private ServiceCodeMapper codeMapper;

    @Autowired
    private ServiceRefuseMapper refuseMapper;

    @Autowired
    private ServiceOrdernurseMapper ordernurseMapper;

    @Autowired
    private ServiceServedMapper servedMapper;

    @Autowired
    private ServiceUsersMapper usersMapper;

    @Autowired
    private ServiceReasonrefuseMapper reasonrefuseMapper;

    @Autowired
    private ServiceOrderotherpayMapper orderotherpayMapper;

    @Autowired
    private ServiceTimeMapper timeMapper;

    @Qualifier("hospitalRestTemplate")
    @Autowired
    private RestTemplate restTemplateHospital;

    @Autowired
    private IServiceAppinfoService appinfoService;

    @Autowired
    private ServiceMsginfoMapper msginfoMapper;

    @Autowired
    private ServiceUsermsgMapper usermsgMapper;

    @Autowired
    private ServiceClockinMapper clockinMapper;


    @Transactional //可能不需要
    public List<ItemComboReturnHospital> initOrder(LoginHospital oldHospital)
    {
        List<ItemComboReturnHospital> retItemComboPackage = new ArrayList<ItemComboReturnHospital>();
        try {
            List<ServiceMycart> mycarts = mycartMapper.selectServiceMycartByMycartUserId(oldHospital.getUserId());
            for (ServiceMycart mycart : mycarts) {
                ItemComboReturnHospital retNode = new ItemComboReturnHospital();
                ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(mycart.getMycartItemid());
                ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
                retNode.setItem(item);
                ServiceItemscombo searchItemsCombo = new ServiceItemscombo();
                searchItemsCombo.setItemscomboItemid(item.getItemId());
                List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(searchItemsCombo);
                ArrayList<ComboPackageReturnHospital> retChlComboList = new ArrayList<>();
                for (ServiceItemscombo itemCombo : itemscomboList){
                    ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

                    ServicePackagescombo searchPackagesCombo = new ServicePackagescombo();
                    searchPackagesCombo.setPackagescomboComboid(combo.getComboId());
                    List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(searchPackagesCombo);
                    ComboPackageReturnHospital newChlNode = new ComboPackageReturnHospital();
                    ArrayList<ServicePackages> packagesList = new ArrayList<>();
                    for (ServicePackagescombo packagescombo : packagescomboList) {
                        ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                        packagesList.add(packages);
                    }
                    newChlNode.setPackages(packagesList);
                    newChlNode.setCombo(combo);
                    retChlComboList.add(newChlNode);
                }
                retNode.setComboPackages(retChlComboList);
                retItemComboPackage.add(retNode);

            }
            return retItemComboPackage;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public Page<HospitalOrderReturnVO> getOrderList(String labelStatus, Long nurseId, int page, int size) {
        List<HospitalOrderReturn> retOrderList = new ArrayList<>();
        List<HospitalOrderReturnVO> retOrderListVO = new ArrayList<HospitalOrderReturnVO>();
        try {
            ServiceNurse nurse = nurseMapper.selectServiceNurseByNurseId(nurseId);
            ServiceOrders searchOrder = new ServiceOrders();
            List<ServiceOrdernurse> orderNurseListSearch = new ArrayList<ServiceOrdernurse>();
            if (nurse.getNurseRoleid() == 2) {
                orderNurseListSearch = ordernurseMapper.selectServiceOrdernurseListByNurseId(nurseId);
            }
            //searchOrder.setOrderDelFlag("0");
            
            searchOrder.setOrderDelFlag("0");

            pictureMapper.selectServicePictureByPictureId(1L);

            Long[] orderServiceFlags = null;
            if (labelStatus.equals("new")){
                orderServiceFlags = new Long[]{5L,6L,7L};
            } else if (labelStatus.equals("unserved")) {
                orderServiceFlags = new Long[]{8L};
            } else if (labelStatus.equals("serving")) {
                orderServiceFlags = new Long[]{9L,10L,11L};
            } else if (labelStatus.equals("unsettled")) {
                orderServiceFlags = new Long[]{12L,13L};
            }else if (labelStatus.equals("canceled")){
                orderServiceFlags = new Long[]{2L,4L};
            }else {
                orderServiceFlags = new Long[]{1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,17L};
            }

            List<ServiceOrders> ordersList = new ArrayList<ServiceOrders>();
            for (Long flag : orderServiceFlags) {
                searchOrder.setOrderServiceflag(Math.toIntExact(flag));
                if (nurse.getNurseRoleid() == 2){
                    for (ServiceOrdernurse ordernurse : orderNurseListSearch){
                        searchOrder.setOrderId(ordernurse.getOrdernurseOrderid());
                        List<ServiceOrders> ordersAll = ordersMapper.selectServiceOrdersList(searchOrder);
                        for (ServiceOrders order : ordersAll){
                            if(order.getOrderPay() != null) {
                                ordersList.add(order);
                            }
                        }
                    }
                }
                if (nurse.getNurseRoleid() == 1){
                    List<ServiceOrders> ordersAll = ordersMapper.selectServiceOrdersList(searchOrder);
                    for (ServiceOrders order : ordersAll){
                        if(order.getOrderPay() != null) {
                            ordersList.add(order);
                        }
                    }
                }
            }

            for (ServiceOrders order : ordersList) {

                if (nurse.getNurseRoleid() == 1 && order.getOrderServiceflag() == 5){
                    addOrderProcess(order.getOrderId());
                }

                HospitalOrderReturnVO retOrderVONode = new HospitalOrderReturnVO();
                Long prePaid = 0L;

                //订单信息
                retOrderVONode.init(order);

                ServicePay payInfo = payMapper.selectServicePayByPayId(order.getOrderPay());
                //支付信息
                if(payInfo != null) retOrderVONode.initPay(payInfo);

                ServiceCode code = codeMapper.selectServiceCodeByOrderId(order.getOrderId());
                //验证码
                //if(code != null) retOrderVONode.setVerifiCode(code.getCodeCode());

                List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(order.getOrderId());
                for (ServiceOrderscombo orderscombo : orderscomboList) {
                    HospitalOrderInfo retVONode = new HospitalOrderInfo();
                    ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(orderscombo.getOrderscomboComboid());
                    ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
                    ServiceCombo combo = comboMapper.selectServiceComboByComboIdNew(orderscombo.getOrderscomboComboid());
                    retVONode.initItem(item);//项目信息录入
                    Long price = 0L;
                    retVONode.setSpecId(combo.getComboId());
                    retVONode.setSpecName(combo.getComboName());
                    retVONode.setCount(orderscomboList.get(0).getOrderscomboCount());
                    price += combo.getComboPrice();

                    List<ServicePackagescombo> packagescombos = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
                    for (ServicePackagescombo packagescombo : packagescombos){
                        ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                        ServiceOrderspackages searchOrdersPackages = new ServiceOrderspackages();
                        searchOrdersPackages.setOrderspackagesOrderid(order.getOrderId());
                        searchOrdersPackages.setOrderspackagesPackageid(packages.getPackagesId());
                        searchOrdersPackages.setOrderspackagesDelFlag("0");
                        List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectServiceOrderspackagesList(searchOrdersPackages);
                        if (orderspackagesList.size() > 0){
                            retVONode.setPkgId(packages.getPackagesId());
                            retVONode.setPkgName(packages.getPackagesName());
                            price += packages.getPackagesPrice();
                            break;
                        }


                    }

                    retVONode.setPrice(price);
                    retVONode.setCost(price * retVONode.getCount());
                    prePaid += retVONode.getCost();

                    if (retOrderVONode.getOrderInfo() == null){
                        retOrderVONode.setOrderInfo(new ArrayList<HospitalOrderInfo>());
                    }
                    retOrderVONode.getOrderInfo().add(retVONode);

                    int buttonFlag = 0;
                    int addBTFlag = 0;
                    List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectOrderspackagesByOrderId(order.getOrderId());
                    ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(order.getOrderId());
                    if (retOrderVONode.getButtons() == null){
                        retOrderVONode.setButtons(new ArrayList<HospitalOrderButton>());
                    }
                    //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                    //    if(button.getType().equals("orderDetail")){
                    //        addBTFlag = 1;
                    //        break;
                    //    }
                    //}
                    //if(addBTFlag == 0){
                    //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
                    //    addBTFlag = 0;buttonFlag++;
                    //}
                    if(nurse.getNurseRoleid() == 1){
                        if(orders.getOrderServiceflag() == 5){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toDispatch")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toDispatch", "派单"));
                                addBTFlag = 0;buttonFlag++;
                            }
                        }
                        if(orders.getOrderServiceflag() == 5){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toReject")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
                                addBTFlag = 0;buttonFlag++;
                            }
                        }
                        //if(orders.getOrderStatus() == 5){
                        //    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                        //        if(button.getType().equals("toSend")){
                        //            addBTFlag = 1;
                        //            break;
                        //        }
                        //    }
                        //    if(addBTFlag == 0){
                        //        retOrderVONode.getButtons().add(new HospitalOrderButton("toSend", "派单"));
                        //        addBTFlag = 0;buttonFlag++;
                        //    }
                        //
                        //}
                    }
                    else if(nurse.getNurseRoleid() == 2){
                        //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                        //    if(button.getType().equals("orderDetail")){
                        //        addBTFlag = 1;
                        //        break;
                        //    }
                        //}
                        //if(addBTFlag == 0){
                        //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
                        //    addBTFlag = 0;buttonFlag++;
                        //}
                        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toAccept")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toAccept", "接单"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toReject")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if(orders.getOrderServiceflag() >= 8 && orders.getOrderServiceflag() <= 11){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("reback")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("reback", "订单转介"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if(orders.getOrderServiceflag() == 8 ){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toLeave")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toLeave", "出发"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        //if(orders.getOrderServiceflag() > 8 && orders.getOrderServiceflag() < 12){
                        //    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                        //        if(button.getType().equals("toLocate")){
                        //            addBTFlag = 1;
                        //            break;
                        //        }
                        //    }
                        //    ServiceClockin clockin = new ServiceClockin();
                        //    clockin.setClockinOrder(order.getOrderId());
                        //    clockin.setClockinNurse(nurseId);
                        //
                        //    List<ServiceClockin> oldClock = clockinMapper.selectServiceClockinList(clockin);
                        //    if(addBTFlag == 0 && !(oldClock.size() > 0)){
                        //        retOrderVONode.getButtons().add(new HospitalOrderButton("toLocate", "定位打卡"));
                        //        addBTFlag = 0;buttonFlag++;
                        //    }
                        //
                        //}
                        if(orders.getOrderServiceflag() == 9 ){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toArrive")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toArrive", "抵达"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if(orders.getOrderServiceflag() == 10){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toFinish")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toFinish", "服务完成"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if(orders.getOrderServiceflag() == 11){
                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                if(button.getType().equals("toCheck")){
                                    addBTFlag = 1;
                                    break;
                                }
                            }
                            if(addBTFlag == 0){
                                retOrderVONode.getButtons().add(new HospitalOrderButton("toCheck", "核对账单"));
                                addBTFlag = 0;buttonFlag++;
                            }

                        }
                        if (buttonFlag < 4){
                            if(orders.getOrderAssessmentid() == null){
                                if(orders.getOrderServiceflag() >= 11){
                                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                        if(button.getType().equals("toRate")){
                                            addBTFlag = 1;
                                            break;
                                        }
                                    }
                                    if(addBTFlag == 0){
                                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
                                        addBTFlag = 0;buttonFlag++;
                                    }
                                }
                            }else{
                                ServiceAssessment assessment = assessmentMapper.selectServiceAssessmentByAssessmentId(orders.getOrderAssessmentid());
                                if("1".equals(assessment.getAssessmentNursestatus()) && orders.getOrderServiceflag() >= 11){
                                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                        if(button.getType().equals("toRate")){
                                            addBTFlag = 1;
                                            break;
                                        }
                                    }
                                    if(addBTFlag == 0){
                                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
                                        addBTFlag = 0;buttonFlag++;
                                    }
                                }
                            }
                        }


                    }

                    //if(orders.getOrderServiceflag() >= 15){
                    //    retOrderVONode.getButtons().add(new HospitalOrderButton("toOrder", "再次购买"));
                    //}


                    if(retOrderVONode.getPrePaid() == null){
                        retOrderVONode.setPrePaid(prePaid + 100);//一百块往返路程费（押金）
                    }
                    List<ServicePicture> medicals = pictureMapper.selectMedicalListByOrderId(order.getOrderId());
                    HospitalProofsInfo proofsInfo = new HospitalProofsInfo();
                    proofsInfo.setMedicalImages(new ArrayList<String>());
                    proofsInfo.setConsumeImages(new ArrayList<String>());
                    for (ServicePicture medical : medicals){
                        proofsInfo.getMedicalImages().add(medical.getPicturePic());
                    }
                    List<ServicePicture> consumes = pictureMapper.selectConsumeListByOrderId(order.getOrderId());
                    for (ServicePicture consume : consumes){
                        proofsInfo.getConsumeImages().add(consume.getPicturePic());
                    }
                    if (medicals.size() == 0) proofsInfo.setMedicalTips("请联系用户上传处方、病历等就医证明");
                    if (consumes.size() == 0) proofsInfo.setConsumeTips("请联系用户上传耗材证明");

                    for(ServiceOrderspackages orderspackages : orderspackagesList){
                        if (orderspackages.getOrderspackagesPackageid() == 0){
                            proofsInfo.setNeedUploadConsume(true);
                            break;
                        }
                    }
                    retOrderVONode.setProofsInfo(proofsInfo);

                    HospitalDispatchInfo dispatchInfo = new HospitalDispatchInfo();

                    List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByOrderId(order.getOrderId());
                    List<HospitalNurseInfo> nurseInfoList = new ArrayList<HospitalNurseInfo>();
                    for (ServiceOrdernurse ordernurse : ordernurseList){
                        ServiceNurse nurseNew = nurseMapper.selectServiceNurseByNurseId(ordernurse.getOrdernurseNurseid());
                        if (nurseNew != null){
                            HospitalNurseInfo nurseInfo = new HospitalNurseInfo();

                            nurseInfo.setNurseId(nurseNew.getNurseId());
                            nurseInfo.setNurseName(nurseNew.getNurseName());
                            nurseInfo.setNurseTel(nurseNew.getNurseTelephonenumber());

                            nurseInfoList.add(nurseInfo);
                        }

                    }
                    dispatchInfo.setNurseInfo(nurseInfoList);

                    HospitalHospInfo hospInfo = new HospitalHospInfo();
                    dispatchInfo.setHospInfo(hospInfo);
                    retOrderVONode.setDispatchInfo(dispatchInfo);

                }

////OLD
//                List<ServiceOrdersitems> ordersitemsList = ordersitemsMapper.selectServiceOrdersitemsByOrderId(order.getOrderId());
//                for (ServiceOrdersitems ordersitems : ordersitemsList) {//项目、套餐、耗材包
//                    HospitalOrderInfo retVONode = new HospitalOrderInfo();
//                    List<ServiceItemscombo> itemscombos = itemscomboMapper.selectByItemId(ordersitems.getOrdersitemsItemid());
//                    ServiceItems item = itemsMapper.selectServiceItemsByItemId(ordersitems.getOrdersitemsItemid());
//                    retVONode.initItem(item);//项目信息录入
//
//                    for (ServiceItemscombo itemscombo : itemscombos){
//                        Long price = 0L;
//                        ServiceOrderscombo searchOrdersCombo = new ServiceOrderscombo();
//                        searchOrdersCombo.setOrderscomboOrderid(order.getOrderId());
//                        searchOrdersCombo.setOrderscomboComboid(itemscombo.getItemscomboSuppliesid());
//                        searchOrdersCombo.setOrderscomboDelFlag("0");
//                        List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboList(searchOrdersCombo);
//                        if (orderscomboList.size() > 0){
//                            ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemscombo.getItemscomboSuppliesid());
//                            retVONode.setSpecId(combo.getComboId());
//                            retVONode.setSpecName(combo.getComboName());
//                            retVONode.setCount(orderscomboList.get(0).getOrderscomboCount());
//                            price += combo.getComboPrice();
//
//                            List<ServicePackagescombo> packagescombos = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
//                            for (ServicePackagescombo packagescombo : packagescombos){
//                                ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
//                                ServiceOrderspackages searchOrdersPackages = new ServiceOrderspackages();
//                                searchOrdersPackages.setOrderspackagesOrderid(order.getOrderId());
//                                searchOrdersPackages.setOrderspackagesPackageid(packages.getPackagesId());
//                                searchOrdersPackages.setOrderspackagesDelFlag("0");
//                                List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectServiceOrderspackagesList(searchOrdersPackages);
//                                if (orderspackagesList.size() > 0){
//                                    retVONode.setPkgId(packages.getPackagesId());
//                                    retVONode.setPkgName(packages.getPackagesName());
//                                    price += packages.getPackagesPrice();
//                                    break;
//                                }
//                            }
//
//                            retVONode.setPrice(price);
//                            retVONode.setCost(price * retVONode.getCount());
//                            prePaid += retVONode.getCost();
//                            break;
//
//                        }
//                    }
//                    if (retOrderVONode.getOrderInfo() == null){
//                        retOrderVONode.setOrderInfo(new ArrayList<HospitalOrderInfo>());
//                    }
//                    retOrderVONode.getOrderInfo().add(retVONode);
//
//                    int buttonFlag = 0;
//                    int addBTFlag = 0;
//                    List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectOrderspackagesByOrderId(order.getOrderId());
//                    ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(order.getOrderId());
//                    if (retOrderVONode.getButtons() == null){
//                        retOrderVONode.setButtons(new ArrayList<HospitalOrderButton>());
//                    }
//                    //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                    //    if(button.getType().equals("orderDetail")){
//                    //        addBTFlag = 1;
//                    //        break;
//                    //    }
//                    //}
//                    //if(addBTFlag == 0){
//                    //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
//                    //    addBTFlag = 0;buttonFlag++;
//                    //}
//                    if(nurse.getNurseRoleid() == 1){
//                        if(orders.getOrderServiceflag() == 5){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toDispatch")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toDispatch", "派单"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//                        }
//                        if(orders.getOrderServiceflag() == 5){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toReject")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//                        }
//                        //if(orders.getOrderStatus() == 5){
//                        //    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                        //        if(button.getType().equals("toSend")){
//                        //            addBTFlag = 1;
//                        //            break;
//                        //        }
//                        //    }
//                        //    if(addBTFlag == 0){
//                        //        retOrderVONode.getButtons().add(new HospitalOrderButton("toSend", "派单"));
//                        //        addBTFlag = 0;buttonFlag++;
//                        //    }
//                        //
//                        //}
//                    }
//                    else if(nurse.getNurseRoleid() == 2){
//                        //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                        //    if(button.getType().equals("orderDetail")){
//                        //        addBTFlag = 1;
//                        //        break;
//                        //    }
//                        //}
//                        //if(addBTFlag == 0){
//                        //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
//                        //    addBTFlag = 0;buttonFlag++;
//                        //}
//                        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toAccept")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toAccept", "接单"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toReject")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() >= 8 && orders.getOrderServiceflag() <= 11){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("reback")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("reback", "订单转介"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() == 8 ){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toLeave")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toLeave", "出发"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() == 9 ){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toArrive")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toArrive", "抵达"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() == 10){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toFinish")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toFinish", "服务完成"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if(orders.getOrderServiceflag() == 11){
//                            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                if(button.getType().equals("toCheck")){
//                                    addBTFlag = 1;
//                                    break;
//                                }
//                            }
//                            if(addBTFlag == 0){
//                                retOrderVONode.getButtons().add(new HospitalOrderButton("toCheck", "核对账单"));
//                                addBTFlag = 0;buttonFlag++;
//                            }
//
//                        }
//                        if (buttonFlag < 4){
//                            if(orders.getOrderAssessmentid() == null){
//                                if(orders.getOrderServiceflag() >= 11){
//                                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                        if(button.getType().equals("toRate")){
//                                            addBTFlag = 1;
//                                            break;
//                                        }
//                                    }
//                                    if(addBTFlag == 0){
//                                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
//                                        addBTFlag = 0;buttonFlag++;
//                                    }
//                                }
//                            }else{
//                                ServiceAssessment assessment = assessmentMapper.selectServiceAssessmentByAssessmentId(orders.getOrderAssessmentid());
//                                if("1".equals(assessment.getAssessmentNursestatus()) && orders.getOrderServiceflag() >= 11){
//                                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
//                                        if(button.getType().equals("toRate")){
//                                            addBTFlag = 1;
//                                            break;
//                                        }
//                                    }
//                                    if(addBTFlag == 0){
//                                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
//                                        addBTFlag = 0;buttonFlag++;
//                                    }
//                                }
//                            }
//                        }
//
//
//                    }
//
//                    //if(orders.getOrderServiceflag() >= 15){
//                    //    retOrderVONode.getButtons().add(new HospitalOrderButton("toOrder", "再次购买"));
//                    //}
//
//
//                    if(retOrderVONode.getPrePaid() == null){
//                        retOrderVONode.setPrePaid(prePaid + 100);//一百块往返路程费（押金）
//                    }
//                    List<ServicePicture> medicals = pictureMapper.selectMedicalListByOrderId(order.getOrderId());
//                    HospitalProofsInfo proofsInfo = new HospitalProofsInfo();
//                    proofsInfo.setMedicalImages(new ArrayList<String>());
//                    proofsInfo.setConsumeImages(new ArrayList<String>());
//                    for (ServicePicture medical : medicals){
//                        proofsInfo.getMedicalImages().add(medical.getPicturePic());
//                    }
//                    List<ServicePicture> consumes = pictureMapper.selectConsumeListByOrderId(order.getOrderId());
//                    for (ServicePicture consume : consumes){
//                        proofsInfo.getConsumeImages().add(consume.getPicturePic());
//                    }
//                    if (medicals.size() == 0) proofsInfo.setMedicalTips("请联系用户上传处方、病历等就医证明");
//                    if (consumes.size() == 0) proofsInfo.setConsumeTips("请联系用户上传耗材证明");
//
//                    for(ServiceOrderspackages orderspackages : orderspackagesList){
//                        if (orderspackages.getOrderspackagesPackageid() == 0){
//                            proofsInfo.setNeedUploadConsume(true);
//                            break;
//                        }
//                    }
//                    retOrderVONode.setProofsInfo(proofsInfo);
//
//                    HospitalDispatchInfo dispatchInfo = new HospitalDispatchInfo();
//
//                    List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByOrderId(order.getOrderId());
//                    List<HospitalNurseInfo> nurseInfoList = new ArrayList<HospitalNurseInfo>();
//                    for (ServiceOrdernurse ordernurse : ordernurseList){
//                        ServiceNurse nurseNew = nurseMapper.selectServiceNurseByNurseId(ordernurse.getOrdernurseNurseid());
//                        if (nurseNew != null){
//                            HospitalNurseInfo nurseInfo = new HospitalNurseInfo();
//
//                            nurseInfo.setNurseId(nurseNew.getNurseId());
//                            nurseInfo.setNurseName(nurseNew.getNurseName());
//                            nurseInfo.setNurseTel(nurseNew.getNurseTelephonenumber());
//
//                            nurseInfoList.add(nurseInfo);
//                        }
//
//                    }
//                    dispatchInfo.setNurseInfo(nurseInfoList);
//
//                    HospitalHospInfo hospInfo = new HospitalHospInfo();
//                    dispatchInfo.setHospInfo(hospInfo);
//                    retOrderVONode.setDispatchInfo(dispatchInfo);
//
//
//                }
                retOrderVONode.setServeInfo(getServeInfo(order.getOrderId()));
                retOrderListVO.add(retOrderVONode);
            }

            // 分页逻辑
            Pageable pageable = PageRequest.of(page, size);
            int start = (int) pageable.getOffset();
            int end = Math.min((start + pageable.getPageSize()), retOrderListVO.size());
            List<HospitalOrderReturnVO> pagedItemsList = retOrderListVO.subList(start, end);



            return new PageImpl<>(pagedItemsList, pageable, retOrderList.size());

        } catch (Exception e) {
            return null;
        }
    }

    @Transactional
    public HospitalServeInfo getServeInfo(Long orderId) {
        try {
            ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(orderId);

            ServiceServed served = servedMapper.selectServiceServedByServedId(orders.getOrderServedid());
            ServiceAddress address = addressMapper.selectServiceAddressByAddressId(orders.getOrderAddressid());
            HospitalServeInfo serveInfo = new HospitalServeInfo();
            serveInfo.setServePerson(served.getServedName());
            serveInfo.setServeTel(served.getServedTelephonenumber());
            serveInfo.setServeAddress(address.getAddressInfo());

            ServiceUsers users = usersMapper.selectServiceUsersByUsersId(served.getServedUserid());
            serveInfo.setUsername(users.getUsersName());
            serveInfo.setUserTel(users.getUsersTelephone());
            DateFormat cstFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String serveTime = cstFormate.format(orders.getOrderScheduledtime());
            serveInfo.setServeTime(serveTime);

            return serveInfo;
        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public HospitalActual getActual(Long orderId,LoginHospital oldHospital) {
        try {
            HospitalActual actual = new HospitalActual();
            HospitalServiceItems serviceItems = getServiceItems(orderId, oldHospital);
            HospitalPackageItems packageItems = getPackageItems(orderId, oldHospital);
            HospitalTaxiItems taxiItems = getTaxiItems(orderId, oldHospital);
            HospitalOtherItems otherItems = getOtherItems(orderId, oldHospital);
            actual.setServiceItems(serviceItems);
            actual.setPackageItems(packageItems);
            actual.setTaxiItems(taxiItems);
            actual.setOtherItems(otherItems);

            actual.setTotalAmount(serviceItems.getAmount() + packageItems.getAmount() + taxiItems.getAmount() + otherItems.getAmount());

            return actual;
        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public HospitalServiceItems getServiceItems(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalServiceItems serviceItems = new HospitalServiceItems();
            if (serviceItems.getItems() == null){
                serviceItems.setItems(new ArrayList<HospitalServiceItemsItem>());
            }
            Long amount = 0L;
            ServiceOrders order = ordersMapper.selectServiceOrdersByOrderId(orderId);
            List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(orderId);
            for (ServiceOrderscombo orderscombo : orderscomboList){
                HospitalServiceItemsItem itemNode = new HospitalServiceItemsItem();
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(orderscombo.getOrderscomboComboid());
                itemNode.setName(combo.getComboName());
                itemNode.setPrice(combo.getComboCurrentprice());
                itemNode.setCount(orderscombo.getOrderscomboCount());
                itemNode.setAmount(combo.getComboCurrentprice() * orderscombo.getOrderscomboCount());
                if (orderscombo.getOrderscomboCreateBy() != oldHospital.getUsername()){
                    itemNode.setFromNur(true);
                }
                amount = amount + itemNode.getAmount();
                serviceItems.getItems().add(itemNode);
            }
            serviceItems.setAmount(amount);
            return serviceItems;
        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public HospitalPackageItems getPackageItems(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalPackageItems packageItems = new HospitalPackageItems();
            if (packageItems.getItems() == null){
                packageItems.setItems(new ArrayList<HospitalPackageItemsItem>());
            }
            Long amount = 0L;
            ServiceOrders order = ordersMapper.selectServiceOrdersByOrderId(orderId);
            List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectOrderspackagesByOrderId(orderId);
            for (ServiceOrderspackages orderspackages : orderspackagesList){
                HospitalPackageItemsItem itemNode = new HospitalPackageItemsItem();
                ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(orderspackages.getOrderspackagesPackageid());
                itemNode.setName(packages.getPackagesName());
                itemNode.setPrice(packages.getPackagesPrice());
                itemNode.setCount(orderspackages.getOrderspackagesCount());
                itemNode.setAmount(packages.getPackagesPrice() * orderspackages.getOrderspackagesCount());
                if (orderspackages.getOrderspackagesCreateBy() != oldHospital.getUsername()){
                    itemNode.setFromNur(true);
                }
                amount = amount + itemNode.getAmount();
                packageItems.getItems().add(itemNode);
            }
            packageItems.setAmount(amount);
            return packageItems;
        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public HospitalTaxiItems getTaxiItems(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalTaxiItems taxiItems = new HospitalTaxiItems();
            if (taxiItems.getItems() == null){
                taxiItems.setItems(new ArrayList<HospitalTaxiItemsItem>());
            }
            Long amount = 0L;
            ServicePayroad payroad = payroadMapper.selectServicePayroadByOrderId(orderId);
            HospitalTaxiItemsItem itemNode = new HospitalTaxiItemsItem();
            itemNode.setType("goto");
            itemNode.setAmount(payroad.getPayroadBill());
            amount = amount + payroad.getPayroadBill();
            taxiItems.getItems().add(itemNode);

            itemNode = new HospitalTaxiItemsItem();
            itemNode.setType("back");
            itemNode.setAmount(payroad.getPayroadActualpay());
            amount = amount + payroad.getPayroadActualpay();
            taxiItems.getItems().add(itemNode);

            taxiItems.setAmount(amount);
            return taxiItems;
        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public HospitalOtherItems getOtherItems(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalOtherItems otherItems = new HospitalOtherItems();
            if (otherItems.getItems() == null){
                otherItems.setItems(new ArrayList<HospitalOtherItemsItem>());
            }
            Long amount = 0L;
            List<ServiceOrderotherpay> orderotherpayList = orderotherpayMapper.selectOtherpayByOrderId(orderId);
            for (ServiceOrderotherpay orderotherpay : orderotherpayList){
                HospitalOtherItemsItem itemNode = new HospitalOtherItemsItem();
                itemNode.setName(orderotherpay.getOrderotherpayDesc());
                itemNode.setPrice(orderotherpay.getOrderotherpayPrice());
                itemNode.setCount(orderotherpay.getOrderotherpayCount());
                itemNode.setAmount(orderotherpay.getOrderotherpayPrice() * orderotherpay.getOrderotherpayCount());
                amount = amount + itemNode.getAmount();
                otherItems.getItems().add(itemNode);
            }
            otherItems.setAmount(amount);

            return otherItems;
        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public HospitalPrepaid getPrePaid(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalPrepaid prepaid = new HospitalPrepaid();

            ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(orderId);
            ServicePay pay = payMapper.selectServicePayByPayId(orders.getOrderPay());
            ServicePayroad payroad = payroadMapper.selectServicePayroadByOrderId(orderId);

            prepaid.setTotalAmount(pay.getPayAdvancepay() + payroad.getPayroadAdvancepay());
            prepaid.setSpecAmount(pay.getPayBill());
            prepaid.setPkgAmount(pay.getPayAdvancepay() - pay.getPayBill());
            prepaid.setTaxiAmount(payroad.getPayroadAdvancepay());

            return prepaid;

        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public HospitalDifference getDifference(HospitalActual actual, HospitalPrepaid prepaid,LoginHospital oldHospital) {
        try {

            HospitalDifference difference = new HospitalDifference();
            Long amount = 0L;
            if (difference.getItems() == null){
                difference.setItems(new ArrayList<HospitalDifferenceItem>());
            }
            HospitalDifferenceItem differenceItem = new HospitalDifferenceItem();
            differenceItem.setDesc("服务费差价");
            differenceItem.setType("service");
            differenceItem.setAmount(actual.getServiceItems().getAmount() - prepaid.getSpecAmount());
            amount += differenceItem.getAmount();
            difference.getItems().add(differenceItem);

            differenceItem = new HospitalDifferenceItem();
            differenceItem.setDesc("耗材费差价");
            differenceItem.setType("package");
            differenceItem.setAmount(actual.getPackageItems().getAmount() - prepaid.getPkgAmount());
            amount += differenceItem.getAmount();
            difference.getItems().add(differenceItem);

            differenceItem = new HospitalDifferenceItem();
            differenceItem.setDesc("行程费差价");
            differenceItem.setType("taxi");
            differenceItem.setAmount(actual.getTaxiItems().getAmount() - prepaid.getTaxiAmount());
            amount += differenceItem.getAmount();
            difference.getItems().add(differenceItem);

            differenceItem = new HospitalDifferenceItem();
            differenceItem.setDesc("其他费用差价");
            differenceItem.setType("other");
            differenceItem.setAmount(actual.getOtherItems().getAmount());
            amount += differenceItem.getAmount();
            difference.getItems().add(differenceItem);

            difference.setAmount(amount);

            return difference;

        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public HospitalPaidInfo getPaidInfo(Long orderId,LoginHospital oldHospital) {
        try {

            HospitalPaidInfo paidInfo = new HospitalPaidInfo();

            HospitalPrepaid prePaid = getPrePaid(orderId, oldHospital);
            HospitalActual actual = getActual(orderId, oldHospital);
            HospitalDifference difference = getDifference(actual, prePaid, oldHospital);
            paidInfo.setActual(actual);
            paidInfo.setPrePaid(prePaid);
            paidInfo.setDifference(difference);

            paidInfo.setTotalAmount(prePaid.getTotalAmount());
            if (actual != null) paidInfo.setTotalAmount(actual.getTotalAmount());

            return paidInfo;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional
    public HospitalCheckInfo getCheckInfo(Long orderId,LoginHospital oldHospital) {
        try {
            List<ServicePicture> pictures = pictureMapper.selectCheckListByOrderId(orderId);
            HospitalCheckInfo checkInfo = new HospitalCheckInfo();
            checkInfo.setCheckImages(new ArrayList<String>());
            for (ServicePicture picture : pictures){
                checkInfo.getCheckImages().add(picture.getPicturePic());
                checkInfo.setCheckTips(checkInfo.getCheckTips() + " ; " + picture.getPictureRemark());
            }

            return checkInfo;

        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public List<OrderStatusRsp> getSegmentsMain() {
        try {
            List<OrderStatusRsp> retOrderStatusList = new ArrayList<OrderStatusRsp>();
            OrderStatusRsp orderStatusRsp = new OrderStatusRsp();
            orderStatusRsp.setText("新");
            orderStatusRsp.setType("new");
            orderStatusRsp.setTitle("新订单");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusRsp();
            orderStatusRsp.setText("待");
            orderStatusRsp.setType("unserved");
            orderStatusRsp.setTitle("待服务");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusRsp();
            orderStatusRsp.setText("服");
            orderStatusRsp.setType("serving");
            orderStatusRsp.setTitle("服务中");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusRsp();
            orderStatusRsp.setText("算");
            orderStatusRsp.setType("settling");
            orderStatusRsp.setTitle("结算中");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusRsp();
            orderStatusRsp.setText("退");
            orderStatusRsp.setType("canceled");
            orderStatusRsp.setTitle("已退单");
            retOrderStatusList.add(orderStatusRsp);

            return retOrderStatusList;
        } catch (Exception e) {
            return null;
        }
    }

    @Transactional
    public List<OrderStatusWorkRsp> getSegments() {
        try {
            List<OrderStatusWorkRsp> retOrderStatusList = new ArrayList<OrderStatusWorkRsp>();
            OrderStatusWorkRsp orderStatusRsp = new OrderStatusWorkRsp();
            orderStatusRsp.setLabel("新订单");
            orderStatusRsp.setValue("new");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusWorkRsp();
            orderStatusRsp.setLabel("待服务");
            orderStatusRsp.setValue("unserved");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusWorkRsp();
            orderStatusRsp.setLabel("服务中");
            orderStatusRsp.setValue("serving");
            retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusWorkRsp();
            orderStatusRsp.setLabel("待结算");
            orderStatusRsp.setValue("unsettled");
            retOrderStatusList.add(orderStatusRsp);

            //orderStatusRsp = new OrderStatusWorkRsp();
            //orderStatusRsp.setLabel("已结算");
            //orderStatusRsp.setValue("settled");
            //retOrderStatusList.add(orderStatusRsp);

            orderStatusRsp = new OrderStatusWorkRsp();
            orderStatusRsp.setLabel("已退单");
            orderStatusRsp.setValue("canceled");
            retOrderStatusList.add(orderStatusRsp);

            return retOrderStatusList;
        } catch (Exception e) {
            return null;
        }
    }

    @Transactional
    public Page<HospitalOrderReturn> getOrderListByNurseId(Long nurseId, int page, int size) {
        List<HospitalOrderReturn> retOrderList = new ArrayList<>();
        try {
            ServiceNurse nurse = nurseMapper.selectServiceNurseByNurseId(nurseId);
            ServiceOrders searchOrder = new ServiceOrders();
            searchOrder.setOrderNurseid(nurseId);
            //searchOrder.setOrderDelFlag("0");
            List<ServiceOrders> ordersList = ordersMapper.selectServiceOrdersList(searchOrder);

            for (ServiceOrders order : ordersList) {
                HospitalOrderReturn retOrderNode = new HospitalOrderReturn();
                retOrderNode.setOrder(order); // 订单信息
                retOrderNode.setServicePay(payMapper.selectServicePayByPayId(order.getOrderPay())); // 支付信息
                retOrderNode.setItemComboReturns(processOrderItems(order.getOrderId()));
                retOrderNode.setActionFlag(getOrderStatusList(order.getOrderServiceflag(), nurse.getNurseRoleid()));
                retOrderList.add(retOrderNode);
            }

            // 分页逻辑
            Pageable pageable = PageRequest.of(page, size);
            int start = (int) pageable.getOffset();
            int end = Math.min((start + pageable.getPageSize()), retOrderList.size());
            List<HospitalOrderReturn> hospitalOrderReturns = retOrderList.subList(start, end);

            return new PageImpl<>(hospitalOrderReturns, pageable, retOrderList.size());

        } catch (Exception e) {
            return null;
        }
    }

    private List<ItemComboReturnHospital> processOrderItems(Long orderId) {
        List<ItemComboReturnHospital> retItemComboPackage = new ArrayList<>();
        List<ServiceOrdersitems> ordersitemsList = ordersitemsMapper.selectServiceOrdersitemsByOrderId(orderId);

        for (ServiceOrdersitems ordersitems : ordersitemsList) { // 项目套餐耗材包
            ItemComboReturnHospital retNode = new ItemComboReturnHospital();
            ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(ordersitems.getOrdersitemsItemid());
            ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
            retNode.setItem(item);
            retNode.setComboPackages(processComboPackages(item.getItemId()));
            retItemComboPackage.add(retNode);
        }

        return retItemComboPackage;
    }

    private List<ComboPackageReturnHospital> processComboPackages(Long itemId) {
        ArrayList<ComboPackageReturnHospital> retChlComboList = new ArrayList<>();
        ServiceItemscombo searchItemsCombo = new ServiceItemscombo();
        searchItemsCombo.setItemscomboItemid(itemId);
        List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(searchItemsCombo);

        for (ServiceItemscombo itemCombo : itemscomboList) {
            ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

            ServicePackagescombo searchPackagesCombo = new ServicePackagescombo();
            searchPackagesCombo.setPackagescomboComboid(combo.getComboId());
            List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(searchPackagesCombo);
            ComboPackageReturnHospital newChlNode = new ComboPackageReturnHospital();
            ArrayList<ServicePackages> packagesList = new ArrayList<>();

            for (ServicePackagescombo packagescombo : packagescomboList) {
                ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                packagesList.add(packages);
            }

            newChlNode.setPackages(packagesList);
            newChlNode.setCombo(combo);
            retChlComboList.add(newChlNode);
        }

        return retChlComboList;
    }


    public List<Map<String, String>> getOrderStatusList(int orderStatus, Long role) {
        List<Map<String, String>> statusList = new ArrayList<>();

        Map<String, String> statusMap = new HashMap<>();
        statusMap.put("status", "0");
        statusMap.put("description", "订单详情");
        statusList.add(statusMap);

        if (role == 1) { // 护理服务小组
            if (orderStatus == 5) {
                statusMap = new HashMap<>();
                statusMap.put("status", "1");
                statusMap.put("description", "派单");
                statusList.add(statusMap);
            }
        } else if (role == 2) { // 护士角色
            statusMap = new HashMap<>();
            switch (orderStatus) {
                case 7:
                    statusMap.put("status", "2");
                    statusMap.put("description", "接单");
                    statusList.add(statusMap);
                    statusMap = new HashMap<>();
                    statusMap.put("status", "3");
                    statusMap.put("description", "拒绝");
                    break;
                case 8:
                    statusMap.put("status", "4");
                    statusMap.put("description", "转介");
                    statusList.add(statusMap);
                    statusMap = new HashMap<>();
                    statusMap.put("status", "5");
                    statusMap.put("description", "出发");
                    break;
                case 9:
                    statusMap.put("status", "4");
                    statusMap.put("description", "转介");
                    statusList.add(statusMap);
                    statusMap = new HashMap<>();
                    statusMap.put("status", "6");
                    statusMap.put("description", "抵达");
                    break;
                case 10:
                    statusMap.put("status", "7");
                    statusMap.put("description", "服务完成");
                    break;
                case 11:
                    statusMap.put("status", "8");
                    statusMap.put("description", "评价");
                    statusList.add(statusMap);
                    statusMap = new HashMap<>();
                    statusMap.put("status", "9");
                    statusMap.put("description", "核对账单");
                    break;
                case 12:
                    statusMap.put("status", "8");
                    statusMap.put("description", "评价");
                    break;
            }
            statusList.add(statusMap);
        }
        return statusList;
    }

    @Transactional //已创建另外--需要
    public List<HospitalOrderReturn> getOrderListAll(Long userId){
        List<HospitalOrderReturn> retOrderList = new ArrayList<HospitalOrderReturn>();
        try {
            ServiceOrders searchOrder = new ServiceOrders();
            searchOrder.setOrderUserid(userId);
            searchOrder.setOrderDelFlag("0");
            List<ServiceOrders> ordersList = ordersMapper.selectServiceOrdersList(searchOrder);
            for (ServiceOrders order : ordersList) {
                List<ItemComboReturnHospital> retItemComboPackage = new ArrayList<ItemComboReturnHospital>();
                HospitalOrderReturn retOrderNode = new HospitalOrderReturn();
                retOrderNode.setOrder(order);//订单信息
                ServicePay payInfo = payMapper.selectServicePayByPayId(order.getOrderPay());
                retOrderNode.setServicePay(payInfo);//支付信息
                ServiceCode code = codeMapper.selectServiceCodeByOrderId(order.getOrderId());

                List<ServiceOrdersitems> ordersitemsList = ordersitemsMapper.selectServiceOrdersitemsByOrderId(order.getOrderId());
                for (ServiceOrdersitems ordersitems : ordersitemsList) {//项目套餐耗材包
                    ItemComboReturnHospital retNode = new ItemComboReturnHospital();
                    ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(ordersitems.getOrdersitemsItemid());
                    ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
                    retNode.setItem(item);
                    ServiceItemscombo searchItemsCombo = new ServiceItemscombo();
                    searchItemsCombo.setItemscomboItemid(item.getItemId());
                    List<ServiceItemscombo> itemscomboList = itemscomboMapper.selectServiceItemscomboList(searchItemsCombo);
                    ArrayList<ComboPackageReturnHospital> retChlComboList = new ArrayList<>();
                    for (ServiceItemscombo itemCombo : itemscomboList){
                        ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemCombo.getItemscomboSuppliesid());

                        ServicePackagescombo searchPackagesCombo = new ServicePackagescombo();
                        searchPackagesCombo.setPackagescomboComboid(combo.getComboId());
                        List<ServicePackagescombo> packagescomboList = packagescomboMapper.selectServicePackagescomboList(searchPackagesCombo);
                        ComboPackageReturnHospital newChlNode = new ComboPackageReturnHospital();
                        ArrayList<ServicePackages> packagesList = new ArrayList<>();
                        for (ServicePackagescombo packagescombo : packagescomboList) {
                            ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                            packagesList.add(packages);
                        }
                        newChlNode.setPackages(packagesList);
                        newChlNode.setCombo(combo);
                        retChlComboList.add(newChlNode);
                    }
                    retNode.setComboPackages(retChlComboList);
                    retItemComboPackage.add(retNode);
                }
                retOrderNode.setItemComboReturns(retItemComboPackage);
                retOrderList.add(retOrderNode);
            }
            return retOrderList;

        }catch (Exception e){
            return null;
        }
    }

    @Transactional //需要
    public HospitalOrderDetailRet getOrderDetial(Long orderId, LoginHospital oldHospital)
    {
        try {

            ServiceOrders order = ordersMapper.selectServiceOrdersByOrderId(orderId);
            if (order == null){
                throw new Exception("未找到订单信息");
            }

            HospitalOrderReturn retOrderNode = new HospitalOrderReturn();
            HospitalOrderDetailRet retOrderVONode = new HospitalOrderDetailRet();
            Long prePaid = 0L;

            retOrderNode.setOrder(order);//订单信息
            retOrderVONode.init(order);

            //ServicePay payInfo = payMapper.selectServicePayByPayId(order.getOrderPay());
            //if(payInfo != null) retOrderNode.setServicePay(payInfo);//支付信息
            //
            //payroadMapper;

            //if(payInfo != null) retOrderVONode.initPay(payInfo);

            //ServiceCode code = codeMapper.selectServiceCodeByOrderId(order.getOrderId());
            //if(code != null) retOrderNode.setCode(code.getCodeCode());//验证码
            //if(code != null) retOrderVONode.setVerifiCode(code.getCodeCode());

            List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(order.getOrderId());
            for (ServiceOrderscombo orderscombo : orderscomboList) {
                HospitalOrderInfo retVONode = new HospitalOrderInfo();
                ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(orderscombo.getOrderscomboComboid());
                ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
                ServiceCombo combo = comboMapper.selectServiceComboByComboIdNew(orderscombo.getOrderscomboComboid());
                retVONode.initItem(item);//项目信息录入
                Long price = 0L;
                retVONode.setSpecId(combo.getComboId());
                retVONode.setSpecName(combo.getComboName());
                retVONode.setSpecPrice(combo.getComboCurrentprice());
                retVONode.setCount(orderscomboList.get(0).getOrderscomboCount());
                price += combo.getComboPrice();

                List<ServicePackagescombo> packagescombos = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
                for (ServicePackagescombo packagescombo : packagescombos) {
                    ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
                    ServiceOrderspackages searchOrdersPackages = new ServiceOrderspackages();
                    searchOrdersPackages.setOrderspackagesOrderid(order.getOrderId());
                    searchOrdersPackages.setOrderspackagesPackageid(packages.getPackagesId());
                    searchOrdersPackages.setOrderspackagesDelFlag("0");
                    List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectServiceOrderspackagesList(searchOrdersPackages);
                    if (orderspackagesList.size() > 0) {
                        retVONode.setPkgId(packages.getPackagesId());
                        retVONode.setPkgName(packages.getPackagesName());
                        retVONode.setPkgAmount(packages.getPackagesPrice());
                        price += packages.getPackagesPrice();
                        break;
                    }


                }

                retVONode.setPrice(price);
                retVONode.setCost(price * retVONode.getCount());
                prePaid += retVONode.getCost();
                retVONode.setFromNur(false);
                ServiceUsers users = usersMapper.selectServiceUsersByUsersId(order.getOrderUserid());
                if (!orderscombo.getOrderscomboCreateBy().equals(users.getUsersName())){
                    retVONode.setFromNur(true);
                }

                if (retOrderVONode.getOrderInfo() == null){
                    retOrderVONode.setOrderInfo(new ArrayList<HospitalOrderInfo>());
                }
                retOrderVONode.getOrderInfo().add(retVONode);

                int buttonFlag = 0;
                int addBTFlag = 0;
                ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(orderId);
                List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectOrderspackagesByOrderId(order.getOrderId());

                if (retOrderVONode.getButtons() == null){
                    retOrderVONode.setButtons(new ArrayList<HospitalOrderButton>());
                }
                //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                //    if(button.getType().equals("orderDetail")){
                //        addBTFlag = 1;
                //        break;
                //    }
                //}
                //if(addBTFlag == 0){
                //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
                //    addBTFlag = 0;buttonFlag++;
                //}
                if(oldHospital.getUser().getNurseRoleid() == 1){
                    if(orders.getOrderServiceflag() == 5){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toDispatch")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toDispatch", "派单"));
                            addBTFlag = 0;buttonFlag++;
                        }
                    }
                    if(orders.getOrderServiceflag() == 5){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toReject")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
                            addBTFlag = 0;buttonFlag++;
                        }
                    }
                    //if(orders.getOrderStatus() == 5){
                    //    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                    //        if(button.getType().equals("toSend")){
                    //            addBTFlag = 1;
                    //            break;
                    //        }
                    //    }
                    //    if(addBTFlag == 0){
                    //        retOrderVONode.getButtons().add(new HospitalOrderButton("toSend", "派单"));
                    //        addBTFlag = 0;buttonFlag++;
                    //    }
                    //
                    //}
                }
                else if(oldHospital.getUser().getNurseRoleid() == 2){
                    //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                    //    if(button.getType().equals("orderDetail")){
                    //        addBTFlag = 1;
                    //        break;
                    //    }
                    //}
                    //if(addBTFlag == 0){
                    //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
                    //    addBTFlag = 0;buttonFlag++;
                    //}
                    if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toAccept")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toAccept", "接单"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toReject")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() >= 8 && orders.getOrderServiceflag() <= 11){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("reback")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("reback", "订单转介"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() == 8 ){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toLeave")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toLeave", "出发"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() > 8 && orders.getOrderServiceflag() < 12){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toLocate")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        ServiceClockin clockin = new ServiceClockin();
                        clockin.setClockinOrder(order.getOrderId());
                        clockin.setClockinNurse(oldHospital.getUserId());

                        List<ServiceClockin> oldClock = clockinMapper.selectServiceClockinList(clockin);
                        if(addBTFlag == 0 && !(oldClock.size() > 0)){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toLocate", "定位打卡"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() == 9 ){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toArrive")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toArrive", "抵达"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() == 10){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toFinish")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toFinish", "服务完成"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if(orders.getOrderServiceflag() == 11){
                        for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                            if(button.getType().equals("toCheck")){
                                addBTFlag = 1;
                                break;
                            }
                        }
                        if(addBTFlag == 0){
                            retOrderVONode.getButtons().add(new HospitalOrderButton("toCheck", "核对账单"));
                            addBTFlag = 0;buttonFlag++;
                        }

                    }
                    if (buttonFlag < 4){
                        if(orders.getOrderAssessmentid() == null){
                            if(orders.getOrderServiceflag() >= 11){
                                for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                    if(button.getType().equals("toRate")){
                                        addBTFlag = 1;
                                        break;
                                    }
                                }
                                if(addBTFlag == 0){
                                    retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
                                    addBTFlag = 0;buttonFlag++;
                                }
                            }
                        }else{
                            ServiceAssessment assessment = assessmentMapper.selectServiceAssessmentByAssessmentId(orders.getOrderAssessmentid());
                            if("1".equals(assessment.getAssessmentNursestatus()) && orders.getOrderServiceflag() >= 11){
                                for (HospitalOrderButton button : retOrderVONode.getButtons()) {
                                    if(button.getType().equals("toRate")){
                                        addBTFlag = 1;
                                        break;
                                    }
                                }
                                if(addBTFlag == 0){
                                    retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
                                    addBTFlag = 0;buttonFlag++;
                                }
                            }
                        }
                    }


                }


                //if(retOrderVONode.getPrePaid() == null){
                //    retOrderVONode.setPrePaid(prePaid + 100);//一百块往返路程费（押金）
                //}

                List<ServicePicture> medicals = pictureMapper.selectMedicalListByOrderId(order.getOrderId());
                HospitalProofsInfo proofsInfo = new HospitalProofsInfo();
                proofsInfo.setMedicalImages(new ArrayList<String>());
                proofsInfo.setConsumeImages(new ArrayList<String>());
                for (ServicePicture medical : medicals){
                    proofsInfo.getMedicalImages().add(medical.getPicturePic());
                }
                List<ServicePicture> consumes = pictureMapper.selectConsumeListByOrderId(order.getOrderId());
                for (ServicePicture consume : consumes){
                    proofsInfo.getConsumeImages().add(consume.getPicturePic());
                }
                if (medicals.size() == 0) proofsInfo.setMedicalTips("请联系用户上传处方、病历等就医证明");
                if (consumes.size() == 0) proofsInfo.setConsumeTips("请联系用户上传耗材证明");
                retOrderVONode.setProofsInfo(proofsInfo);
                for(ServiceOrderspackages orderspackages : orderspackagesList){
                    if (orderspackages.getOrderspackagesPackageid() == 0){
                        proofsInfo.setNeedUploadConsume(true);
                        break;
                    }
                }

                HospitalDispatchInfo dispatchInfo = new HospitalDispatchInfo();
                List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByOrderId(order.getOrderId());
                List<HospitalNurseInfo> nurseInfoList = new ArrayList<HospitalNurseInfo>();
                for (ServiceOrdernurse ordernurse : ordernurseList){
                    ServiceNurse nurse = nurseMapper.selectServiceNurseByNurseId(ordernurse.getOrdernurseNurseid());
                    if (nurse != null){
                        HospitalNurseInfo nurseInfo = new HospitalNurseInfo();

                        nurseInfo.setNurseId(nurse.getNurseId());
                        nurseInfo.setNurseName(nurse.getNurseName());
                        nurseInfo.setNurseTel(nurse.getNurseTelephonenumber());

                        nurseInfoList.add(nurseInfo);
                    }

                }
                dispatchInfo.setNurseInfo(nurseInfoList);

                HospitalHospInfo hospInfo = new HospitalHospInfo();
                dispatchInfo.setHospInfo(hospInfo);
                retOrderVONode.setDispatchInfo(dispatchInfo);

                //被服务信息
                retOrderVONode.setServeInfo(getServeInfo(order.getOrderId()));
                retOrderVONode.setPaidInfo(getPaidInfo(order.getOrderId(),oldHospital));
                retOrderVONode.setCheckInfo(getCheckInfo(order.getOrderId(),oldHospital));

            }


//OLD
            //List<ServiceOrdersitems> ordersitemsList = ordersitemsMapper.selectServiceOrdersitemsByOrderId(order.getOrderId());
            //for (ServiceOrdersitems ordersitems : ordersitemsList) {//项目、套餐、耗材包
            //    HospitalOrderInfo retVONode = new HospitalOrderInfo();
            //    List<ServiceItemscombo> itemscombos = itemscomboMapper.selectByItemId(ordersitems.getOrdersitemsItemid());
            //    ServiceItems item = itemsMapper.selectServiceItemsByItemId(ordersitems.getOrdersitemsItemid());
            //    retVONode.initItem(item);//项目信息录入
            //
            //    for (ServiceItemscombo itemscombo : itemscombos){
            //        Long price = 0L;
            //        ServiceOrderscombo searchOrdersCombo = new ServiceOrderscombo();
            //        searchOrdersCombo.setOrderscomboOrderid(ordersitems.getOrdersitemsOrderid());
            //        searchOrdersCombo.setOrderscomboComboid(itemscombo.getItemscomboSuppliesid());
            //        searchOrdersCombo.setOrderscomboDelFlag("0");
            //        List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboList(searchOrdersCombo);
            //        if (orderscomboList.size() > 0){
            //            ServiceCombo combo = comboMapper.selectServiceComboByComboId(itemscombo.getItemscomboSuppliesid());
            //            retVONode.setSpecId(combo.getComboId());
            //            retVONode.setSpecName(combo.getComboName());
            //            retVONode.setSpecPrice(combo.getComboCurrentprice());
            //            retVONode.setCount(orderscomboList.get(0).getOrderscomboCount());
            //            price += combo.getComboPrice();
            //
            //            List<ServicePackagescombo> packagescombos = packagescomboMapper.selectServicePackagescomboByComboId(combo.getComboId());
            //            for (ServicePackagescombo packagescombo : packagescombos){
            //                ServicePackages packages = packagesMapper.selectServicePackagesByPackagesId(packagescombo.getPackagescomboPackagesid());
            //                ServiceOrderspackages searchOrdersPackages = new ServiceOrderspackages();
            //                searchOrdersPackages.setOrderspackagesOrderid(ordersitems.getOrdersitemsOrderid());
            //                searchOrdersPackages.setOrderspackagesPackageid(packages.getPackagesId());
            //                searchOrdersPackages.setOrderspackagesDelFlag("0");
            //                List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectServiceOrderspackagesList(searchOrdersPackages);
            //                if (orderspackagesList.size() > 0){
            //                    retVONode.setPkgId(packages.getPackagesId());
            //                    retVONode.setPkgName(packages.getPackagesName());
            //                    retVONode.setPkgAmount(packages.getPackagesPrice());
            //                    price += packages.getPackagesPrice();
            //                    break;
            //                }
            //            }
            //
            //            retVONode.setPrice(price);
            //            retVONode.setCost(price * retVONode.getCount());
            //            prePaid += retVONode.getCost();
            //            break;
            //
            //        }
            //    }
            //    if (retOrderVONode.getOrderInfo() == null){
            //        retOrderVONode.setOrderInfo(new ArrayList<HospitalOrderInfo>());
            //    }
            //    retOrderVONode.getOrderInfo().add(retVONode);
            //
            //    int buttonFlag = 0;
            //    int addBTFlag = 0;
            //    ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(ordersitems.getOrdersitemsOrderid());
            //    List<ServiceOrderspackages> orderspackagesList = orderspackagesMapper.selectOrderspackagesByOrderId(order.getOrderId());
            //
            //    if (retOrderVONode.getButtons() == null){
            //        retOrderVONode.setButtons(new ArrayList<HospitalOrderButton>());
            //    }
            //    //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //    //    if(button.getType().equals("orderDetail")){
            //    //        addBTFlag = 1;
            //    //        break;
            //    //    }
            //    //}
            //    //if(addBTFlag == 0){
            //    //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
            //    //    addBTFlag = 0;buttonFlag++;
            //    //}
            //    if(oldHospital.getUser().getNurseRoleid() == 1){
            //        if(orders.getOrderServiceflag() == 5){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toDispatch")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toDispatch", "派单"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //        }
            //        if(orders.getOrderServiceflag() == 5){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toReject")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //        }
            //        //if(orders.getOrderStatus() == 5){
            //        //    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //        //        if(button.getType().equals("toSend")){
            //        //            addBTFlag = 1;
            //        //            break;
            //        //        }
            //        //    }
            //        //    if(addBTFlag == 0){
            //        //        retOrderVONode.getButtons().add(new HospitalOrderButton("toSend", "派单"));
            //        //        addBTFlag = 0;buttonFlag++;
            //        //    }
            //        //
            //        //}
            //    }
            //    else if(oldHospital.getUser().getNurseRoleid() == 2){
            //        //for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //        //    if(button.getType().equals("orderDetail")){
            //        //        addBTFlag = 1;
            //        //        break;
            //        //    }
            //        //}
            //        //if(addBTFlag == 0){
            //        //    retOrderVONode.getButtons().add(new HospitalOrderButton("orderDetail", "订单详情"));
            //        //    addBTFlag = 0;buttonFlag++;
            //        //}
            //        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toAccept")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toAccept", "接单"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() == 6 || orders.getOrderServiceflag() == 7){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toReject")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toReject", "拒绝"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() >= 8 && orders.getOrderServiceflag() <= 11){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("reback")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("reback", "订单转介"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() == 8 ){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toLeave")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toLeave", "出发"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() == 9 ){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toArrive")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toArrive", "抵达"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() == 10){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toFinish")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toFinish", "服务完成"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if(orders.getOrderServiceflag() == 11){
            //            for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                if(button.getType().equals("toCheck")){
            //                    addBTFlag = 1;
            //                    break;
            //                }
            //            }
            //            if(addBTFlag == 0){
            //                retOrderVONode.getButtons().add(new HospitalOrderButton("toCheck", "核对账单"));
            //                addBTFlag = 0;buttonFlag++;
            //            }
            //
            //        }
            //        if (buttonFlag < 4){
            //            if(orders.getOrderAssessmentid() == null){
            //                if(orders.getOrderServiceflag() >= 11){
            //                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                        if(button.getType().equals("toRate")){
            //                            addBTFlag = 1;
            //                            break;
            //                        }
            //                    }
            //                    if(addBTFlag == 0){
            //                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
            //                        addBTFlag = 0;buttonFlag++;
            //                    }
            //                }
            //            }else{
            //                ServiceAssessment assessment = assessmentMapper.selectServiceAssessmentByAssessmentId(orders.getOrderAssessmentid());
            //                if("1".equals(assessment.getAssessmentNursestatus()) && orders.getOrderServiceflag() >= 11){
            //                    for (HospitalOrderButton button : retOrderVONode.getButtons()) {
            //                        if(button.getType().equals("toRate")){
            //                            addBTFlag = 1;
            //                            break;
            //                        }
            //                    }
            //                    if(addBTFlag == 0){
            //                        retOrderVONode.getButtons().add(new HospitalOrderButton("toRate", "评价"));
            //                        addBTFlag = 0;buttonFlag++;
            //                    }
            //                }
            //            }
            //        }
            //
            //
            //    }
            //
            //
            //    //if(retOrderVONode.getPrePaid() == null){
            //    //    retOrderVONode.setPrePaid(prePaid + 100);//一百块往返路程费（押金）
            //    //}
            //
            //    List<ServicePicture> medicals = pictureMapper.selectMedicalListByOrderId(order.getOrderId());
            //    HospitalProofsInfo proofsInfo = new HospitalProofsInfo();
            //    proofsInfo.setMedicalImages(new ArrayList<String>());
            //    proofsInfo.setConsumeImages(new ArrayList<String>());
            //    for (ServicePicture medical : medicals){
            //        proofsInfo.getMedicalImages().add(medical.getPicturePic());
            //    }
            //    List<ServicePicture> consumes = pictureMapper.selectConsumeListByOrderId(order.getOrderId());
            //    for (ServicePicture consume : consumes){
            //        proofsInfo.getConsumeImages().add(consume.getPicturePic());
            //    }
            //    if (medicals.size() == 0) proofsInfo.setMedicalTips("请联系用户上传处方、病历等就医证明");
            //    if (consumes.size() == 0) proofsInfo.setConsumeTips("请联系用户上传耗材证明");
            //    retOrderVONode.setProofsInfo(proofsInfo);
            //    for(ServiceOrderspackages orderspackages : orderspackagesList){
            //        if (orderspackages.getOrderspackagesPackageid() == 0){
            //            proofsInfo.setNeedUploadConsume(true);
            //            break;
            //        }
            //    }
            //
            //    HospitalDispatchInfo dispatchInfo = new HospitalDispatchInfo();
            //    List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByOrderId(order.getOrderId());
            //    List<HospitalNurseInfo> nurseInfoList = new ArrayList<HospitalNurseInfo>();
            //    for (ServiceOrdernurse ordernurse : ordernurseList){
            //        ServiceNurse nurse = nurseMapper.selectServiceNurseByNurseId(ordernurse.getOrdernurseNurseid());
            //        if (nurse != null){
            //            HospitalNurseInfo nurseInfo = new HospitalNurseInfo();
            //
            //            nurseInfo.setNurseId(nurse.getNurseId());
            //            nurseInfo.setNurseName(nurse.getNurseName());
            //            nurseInfo.setNurseTel(nurse.getNurseTelephonenumber());
            //
            //            nurseInfoList.add(nurseInfo);
            //        }
            //
            //    }
            //    dispatchInfo.setNurseInfo(nurseInfoList);
            //
            //    HospitalHospInfo hospInfo = new HospitalHospInfo();
            //    dispatchInfo.setHospInfo(hospInfo);
            //    retOrderVONode.setDispatchInfo(dispatchInfo);
            //
            //    //被服务信息
            //    retOrderVONode.setServeInfo(getServeInfo(order.getOrderId()));
            //    retOrderVONode.setPaidInfo(getPaidInfo(order.getOrderId(),oldHospital));
            //    retOrderVONode.setCheckInfo(getCheckInfo(order.getOrderId(),oldHospital));
            //
            //}
            return retOrderVONode;

        }catch (Exception e){
            return null;
        }
    }

    //@Transactional
    //public boolean newOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    //{
    //    try {
    //        ServiceOrders newOrder = new ServiceOrders();
    //        SnowFlakeH idWorker = new SnowFlakeH(0, 0);
    //        long id = idWorker.nextId();
    //        newOrder.setOrderUserid(oldHospital.getUserId());
    //        newOrder.setOrderCode(String.valueOf(id));
    //        newOrder.setOrderServedid(orderBody.getOrderServedid());
    //        newOrder.setOrderAddressid(orderBody.getOrderAddressid());
    //        newOrder.setOrderServiceflag(1);
    //        newOrder.setOrderCreatetime(new Date());
    //        newOrder.setOrderScheduledtime(orderBody.getOrderScheduledtime());
    //        newOrder.setOrderRemark(orderBody.getOrderRemark());
    //        newOrder.setOrderCreateBy(oldHospital.getUsername());
    //        newOrder.setOrderCreateTime(new Date());
    //        newOrder.setOrderUpdateBy(oldHospital.getUsername());
    //        newOrder.setOrderUpdateTime(new Date());
    //
    //        ordersMapper.insertServiceOrders(newOrder);
    //
    //        ServicePayroad newPayroad = new ServicePayroad();
    //        newPayroad.setPayroadOrderid(newOrder.getOrderId());
    //        newPayroad.setPayroadAdvancepay(orderBody.getPayRoad());
    //        newPayroad.setPayroadStatus("1");
    //        newPayroad.setPayroadStatusfinish("1");
    //        newPayroad.setPayroadCreateBy(oldHospital.getUsername());
    //        newPayroad.setPayroadCreateTime(new Date());
    //        newPayroad.setPayroadUpdateBy(oldHospital.getUsername());
    //        newPayroad.setPayroadUpdateTime(new Date());
    //
    //        payroadMapper.insertServicePayroad(newPayroad);
    //
    //        List<ServiceMycart> mycarts = mycartMapper.selectServiceMycartByMycartUserId(oldHospital.getUserId());
    //        for (ServiceMycart mycart : mycarts) {
    //            ServiceOrderscombo newOrdersCombo = new ServiceOrderscombo();
    //            newOrdersCombo.setOrderscomboComboid(mycart.getMycartItemid());
    //            newOrdersCombo.setOrderscomboCount(Long.valueOf(mycart.getMycartNum()));
    //            newOrdersCombo.setOrderscomboOrderid(newOrder.getOrderId());
    //            newOrdersCombo.setOrderscomboCreateBy(oldHospital.getUsername());
    //            newOrdersCombo.setOrderscomboCreateTime(new Date());
    //            newOrdersCombo.setOrderscomboUpdateBy(oldHospital.getUsername());
    //            newOrdersCombo.setOrderscomboUpdateTime(new Date());
    //            orderscomboMapper.insertServiceOrderscombo(newOrdersCombo);
    //
    //            ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(mycart.getMycartItemid());
    //            ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
    //            ServiceOrdersitems newOrdersItem = new ServiceOrdersitems();
    //            newOrdersItem.setOrdersitemsItemid(item.getItemId());
    //            newOrdersItem.setOrdersitemsCount(Long.valueOf(mycart.getMycartNum()));
    //            newOrdersItem.setOrdersitemsOrderid(newOrder.getOrderId());
    //            newOrdersItem.setOrdersitemsCreateBy(oldHospital.getUsername());
    //            newOrdersItem.setOrdersitemsCreateTime(new Date());
    //            newOrdersItem.setOrdersitemsUpdateBy(oldHospital.getUsername());
    //            newOrdersItem.setOrdersitemsUpdateTime(new Date());
    //            ordersitemsMapper.insertServiceOrdersitems(newOrdersItem);
    //
    //        }
    //    }catch (Exception e){
    //        return false;
    //    }
    //    return true;
    //}
    @Transactional//派单
    public boolean sendOrder(SendOrderReq reqBody, LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(reqBody.getOrderId());

            ServiceOrdernurse newOrderNurse = new ServiceOrdernurse();
            newOrderNurse.setOrdernurseOrderid(reqBody.getOrderId());
            newOrderNurse.setOrdernurseCreateBy(oldHospital.getUsername());
            newOrderNurse.setOrdernurseCreateTime(new Date());
            newOrderNurse.setOrdernurseUpdateBy(oldHospital.getUsername());
            newOrderNurse.setOrdernurseUpdateTime(new Date());

            for (Long nurseId : reqBody.getNurseIds()){
                newOrderNurse.setOrdernurseNurseid(nurseId);
                ordernurseMapper.insertServiceOrdernurse(newOrderNurse);
            }
            newOrder.setOrderNurseid(reqBody.getNurseIds().get(0));
            newOrder.setOrderServiceflag(7);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
            addOrderProcess(newOrder.getOrderId());
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//得到可派单护士
    public List<NurseReturnVO> getNurseList(Long orderId, String name, LoginHospital oldHospital, Integer page, Integer size)
    {
        try {
            List<NurseReturnVO> retNurseList = new ArrayList<NurseReturnVO>();
            if (name == null || name.equals("")){
                List<Long> nurseIds = new ArrayList<>();
                Boolean flag = true;

                List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(orderId);
                for (ServiceOrderscombo orderscombo : orderscomboList){
                    ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(orderscombo.getOrderscomboComboid());
                    List<ServiceNurseitems> nurseitems = nurseitemsMapper.selectServiceNurseitemsListByItemId(itemscombo.getItemscomboItemid());
                    for (ServiceNurseitems nurseitem : nurseitems){
                        for (Long nurseId : nurseIds){
                            if (nurseId.equals(nurseitem.getNurseitemsNurseid())){
                                flag = false;
                            }
                        }
                        if (flag){
                            nurseIds.add(nurseitem.getNurseitemsNurseid());
                            flag = true;
                        }
                    }
                }
                for (Long nurseId : nurseIds){
                    ServiceNurse nurse = nurseMapper.selectServiceNurseByNurseId(nurseId);
                    retNurseList.add(new NurseReturnVO(nurse));
                }
            }else{
                ServiceNurse searchNurse = new ServiceNurse();
                searchNurse.setNurseRoleid(2L);
                searchNurse.setNurseName(name);
                searchNurse.setNurseUseFlag("0");
                searchNurse.setNurseDelFlag("0");
                List<ServiceNurse> nurseList = nurseMapper.selectServiceNurseList(searchNurse);
                for (ServiceNurse nurse : nurseList) {
                    retNurseList.add(new NurseReturnVO(nurse));
                }
            }

            //// 分页逻辑
            //Pageable pageable = PageRequest.of(page, size);
            //int start = (int) pageable.getOffset();
            //int end = Math.min((start + pageable.getPageSize()), retNurseList.size());
            //List<NurseReturnVO> nurseReturnVOS = retNurseList.subList(start, end);

            return retNurseList;
            //return new PageImpl<>(nurseReturnVOS, pageable, retNurseList.size());
        }catch (Exception e){
            return null;
        }
    }
    @Transactional//接单
    public boolean acceptOrder(Long orderId , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            newOrder.setOrderServiceflag(8);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
            addOrderProcess(orderId);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//拒单或转介
    public boolean refuseOrder(Long orderId , Long reason, int type, LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            if (oldHospital.getRoleId() == 1) {
                newOrder.setOrderStatus(1);
                newOrder.setOrderServiceflag(2);
            }
            if (oldHospital.getRoleId() == 2) {
                newOrder.setOrderServiceflag(5);
            }
            //newOrder.setOrderNurseid(null);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ServiceRefuse newRefuse = new ServiceRefuse();
            newRefuse.setRefuseNurseid(oldHospital.getUserId());
            newRefuse.setRefuseOrderid(orderId);

            ServiceReasonrefuse reasonrefuse = reasonrefuseMapper.selectServiceReasonrefuseByReasonrefuseId(reason);
            newRefuse.setRefuseReason(reasonrefuse.getReasonrefuseName());
            newRefuse.setRefuseCreateBy(oldHospital.getUsername());
            newRefuse.setRefuseCreateTime(new Date());
            newRefuse.setRefuseUpdateBy(oldHospital.getUsername());
            newRefuse.setRefuseUpdateTime(new Date());
            switch (type){
                case 0:
                    newRefuse.setRefuseType("0");
                    refuseMapper.insertServiceRefuse(newRefuse);
                    break;
                case 1:
                    newRefuse.setRefuseType("1");
                    refuseMapper.insertServiceRefuse(newRefuse);
                    break;
            }
            List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByNurseId(oldHospital.getUserId());
            for (ServiceOrdernurse ordernurse : ordernurseList){
                if (ordernurse.getOrdernurseOrderid() == orderId){
                    ordernurse.setOrdernurseUseFlag("2");
                    ordernurse.setOrdernurseDelFlag("2");
                    ordernurseMapper.updateServiceOrdernurse(ordernurse);
                }
            }

            ordersMapper.updateServiceOrders(newOrder);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//出发
    public boolean startOrder(Long orderId , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            newOrder.setOrderServiceflag(9);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
            addOrderProcess(orderId);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//到达
    public boolean reachOrder(Long orderId , String code , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            ServiceOrders oldOrder = ordersMapper.selectServiceOrdersByOrderId(orderId);
            ServiceCode oldCode = codeMapper.selectServiceCodeByOrderId(orderId);
            if (!oldCode.getCodeCode().equals(code)){
                throw new Exception("验证码错误!");
            }
            newOrder.setOrderServiceflag(10);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
            addOrderProcess(orderId);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//服务完成
    public boolean endServiceOrder(Long orderId , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            newOrder.setOrderServiceflag(11);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
            addOrderProcess(orderId);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//订单核对信息 大改
    public boolean getCheckOrder(Long orderId , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderId);
            newOrder.setOrderServiceflag(12);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    //@Transactional//不需要
    //public boolean payOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    //{
    //    try {
    //        ServicePay newPay = new ServicePay();
    //        //支付接口
    //        newPay.setPayAdvancepay(orderBody.getPayMoney());
    //        newPay.setPayAdvancepaycode(orderBody.getPayCode());
    //        newPay.setPayAdvancepaytype(orderBody.getPayType());
    //        newPay.setPayStatus("0");
    //        newPay.setPayStatusfinish("1");
    //        newPay.setPayCreateBy(oldHospital.getUsername());
    //        newPay.setPayCreateTime(new Date());
    //        newPay.setPayUpdateBy(oldHospital.getUsername());
    //        newPay.setPayUpdateTime(new Date());
    //        payMapper.insertServicePay(newPay);
    //
    //        ServiceOrders newOrder = new ServiceOrders();
    //        newOrder.setOrderId(orderBody.getOrderId());
    //        newOrder.setOrderPay(newPay.getPayId());
    //        newOrder.setOrderServiceflag(3);
    //        newOrder.setOrderUpdateBy(oldHospital.getUsername());
    //        newOrder.setOrderUpdateTime(new Date());
    //
    //        ordersMapper.updateServiceOrders(newOrder);
    //
    //        Random randObj = new Random();
    //        String code = Integer.toString(1000 + randObj.nextInt(9000));
    //        ServiceCode newCode = new ServiceCode();
    //        newCode.setCodeCode(code);
    //        newCode.setCodeOrderid(orderBody.getOrderId());
    //        newCode.setCodeUserid(oldHospital.getUserId());
    //        newCode.setCodeUseflag("0");
    //        codeMapper.insertServiceCode(newCode);
    //
    //    }catch (Exception e){
    //        return false;
    //    }
    //    return true;
    //}

    @Transactional//需要 退款审核
    public boolean refundOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    {
        try {
            ServicePay newPay = new ServicePay();
            newPay.setPayId(orderBody.getOrderPay());
            newPay.setPayStatus("0");
            newPay.setPayStatusfinish("0");
            newPay.setPayUpdateBy(oldHospital.getUsername());
            newPay.setPayUpdateTime(new Date());
            payMapper.updateServicePay(newPay);

            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderBody.getOrderId());
            newOrder.setOrderPay(newPay.getPayId());
            newOrder.setOrderServiceflag(4);
            newOrder.setOrderFlag(2);
            //退款接口
            newOrder.setOrderFlag(3);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);

        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional//改地址 可能会用到 暂时保留
    public boolean modifyOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    {
        try {
            ServiceOrders newOrder = new ServiceOrders();
            newOrder.setOrderId(orderBody.getOrderId());
            newOrder.setOrderAddressid(orderBody.getOrderAddressid());
            newOrder.setOrderScheduledtime(orderBody.getOrderScheduledtime());
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());

            ordersMapper.updateServiceOrders(newOrder);

        }catch (Exception e){
            return false;
        }
        return true;
    }

    //@Transactional //不需要
    //public boolean payFinishOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    //{
    //    try {
    //        ServicePay oldPay = payMapper.selectServicePayByPayId(orderBody.getOrderPay());
    //
    //        if(oldPay.getPayBill() < oldPay.getPayAdvancepay()){
    //            ServicePay newPay = new ServicePay();
    //            //支付接口
    //            newPay.setPayId(orderBody.getOrderPay());
    //            newPay.setPayFinalpay(orderBody.getPayMoney());
    //            newPay.setPayFinalpaycode(orderBody.getPayCode());
    //            newPay.setPayFinalpaytype(orderBody.getPayType());
    //
    //            newPay.setPayActualpay(oldPay.getPayAdvancepay() + orderBody.getPayMoney());
    //
    //            newPay.setPayStatusfinish("0");
    //            newPay.setPayUpdateBy(oldHospital.getUsername());
    //            newPay.setPayUpdateTime(new Date());
    //            payMapper.updateServicePay(newPay);
    //        }else{
    //            ServicePay newPay = new ServicePay();
    //            //部分退款接口
    //            newPay.setPayId(orderBody.getOrderPay());
    //            newPay.setPayFinalpay(orderBody.getPayMoney());
    //            newPay.setPayFinalpaycode(orderBody.getPayCode());
    //            newPay.setPayFinalpaytype(orderBody.getPayType());
    //
    //            newPay.setPayActualpay(oldPay.getPayAdvancepay() + orderBody.getPayMoney());
    //
    //            newPay.setPayStatusfinish("0");
    //            newPay.setPayUpdateBy(oldHospital.getUsername());
    //            newPay.setPayUpdateTime(new Date());
    //            payMapper.updateServicePay(newPay);
    //        }
    //
    //        ServiceOrders newOrder = new ServiceOrders();
    //        newOrder.setOrderId(orderBody.getOrderId());
    //        newOrder.setOrderServiceflag(14);
    //        newOrder.setOrderUpdateBy(oldHospital.getUsername());
    //        newOrder.setOrderUpdateTime(new Date());
    //
    //        ordersMapper.updateServiceOrders(newOrder);
    //
    //    }catch (Exception e){
    //        return false;
    //    }
    //    return true;
    //}

    @Transactional //订单评价
    public boolean assessOrder(Long orderId, String assessment , LoginHospital oldHospital)
    {
        try {
            ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(orderId);
            ServiceAssessment newAssessment = new ServiceAssessment();
            ServiceOrders newOrder = new ServiceOrders();
            if(orders.getOrderAssessmentid() != null){
                newAssessment.setAssessmentId(orders.getOrderAssessmentid());
                newAssessment.setAssessmentNurse(assessment);
                newAssessment.setAssessmentNursestatus("0");
                newAssessment.setAssessmentUpdateBy(oldHospital.getUsername());
                newAssessment.setAssessmentUpdateTime(new Date());
                assessmentMapper.updateServiceAssessment(newAssessment);
            }else{
                newAssessment.setAssessmentNurse(assessment);
                newAssessment.setAssessmentNursestatus("0");
                newAssessment.setAssessmentCreateBy(oldHospital.getUsername());
                newAssessment.setAssessmentCreateTime(new Date());
                newAssessment.setAssessmentUpdateBy(oldHospital.getUsername());
                newAssessment.setAssessmentUpdateTime(new Date());
                assessmentMapper.insertServiceAssessment(newAssessment);
                newOrder.setOrderAssessmentid(newAssessment.getAssessmentId());
            }

            newOrder.setOrderId(orderId);
            newOrder.setOrderUpdateBy(oldHospital.getUsername());
            newOrder.setOrderUpdateTime(new Date());
            ordersMapper.updateServiceOrders(newOrder);

        }catch (Exception e){
            return false;
        }
        return true;
    }

    //@Transactional //不用
    //public boolean deleteOrder(HospitalOrderBody orderBody , LoginHospital oldHospital)
    //{
    //    try {
    //        ServiceOrders newOrder = new ServiceOrders();
    //        newOrder.setOrderId(orderBody.getOrderId());
    //        newOrder.setOrderUpdateBy(oldHospital.getUsername());
    //        newOrder.setOrderUpdateTime(new Date());
    //
    //        newOrder.setOrderUseFlag("2");
    //        newOrder.setOrderDelFlag("2");
    //
    //        ordersMapper.updateServiceOrders(newOrder);
    //
    //    }catch (Exception e){
    //        return false;
    //    }
    //    return true;
    //}

    @Transactional//改地址 可能会用到 暂时保留
    public List<Map<String, Object>> getNurseReason(LoginHospital oldHospital)
    {
        try {
            List<Map<String, Object>> result = new ArrayList<>();
            ServiceReasonrefuse searchReasonrefuse = new ServiceReasonrefuse();
            searchReasonrefuse.setReasonrefuseUseFlag(String.valueOf(oldHospital.getRoleId()));
            searchReasonrefuse.setReasonrefuseDelFlag("0");

            List<ServiceReasonrefuse> reasonrefuseList = reasonrefuseMapper.selectServiceReasonrefuseList(searchReasonrefuse);
            for(ServiceReasonrefuse reasonrefuse : reasonrefuseList){
                Map<String, Object> reasonrefuseMap = new HashMap<>();
                reasonrefuseMap.put("id", reasonrefuse.getReasonrefuseId());
                reasonrefuseMap.put("reason", reasonrefuse.getReasonrefuseName());
                result.add(reasonrefuseMap);
            }
            return result;

        }catch (Exception e){
            return null;
        }
    }


    @Transactional
    public Boolean addOrderProcess(Long orderId){
        try {
            ServiceOrders orders = ordersMapper.selectServiceOrdersByOrderId(orderId);
            List<ServiceTime> timeList = timeMapper.selectLookByOrderId(orderId);
            if(timeList.size() > 0){
                return false;
            }
            ServiceTime newTime = new ServiceTime();
            String process = "noNotify";

            newTime.setTimeOrderid(orderId);
            newTime.setTimeTime(new Date());

            Integer flag = orders.getOrderServiceflag();
            switch (flag){
                case 1:
                    newTime.setTimeAction("提交订单");
                    newTime.setTimeActionid(1L);
                    break;
                case 2:
                    newTime.setTimeAction("取消订单");
                    newTime.setTimeActionid(13L);
                    if (orders.getOrderPay() != null){
                        process = "订单取消";
                    }
                    break;
                case 3:
                    newTime.setTimeAction("支付订单");
                    newTime.setTimeActionid(2L);
                    break;
                case 4:
                    newTime.setTimeAction("订单退款");
                    newTime.setTimeActionid(4L);
                    if (orders.getOrderPay() == null){
                        process = "订单取消";
                    }else{
                        process = "订单已退款";
                    }
                    break;
                case 5:
                    newTime.setTimeAction("护理部接单");
                    newTime.setTimeActionid(27L);
                    break;
                case 6:
                case 7:
                    newTime.setTimeAction("已分配");
                    newTime.setTimeActionid(23L);
                    process = "已分配护士";
                    break;
                case 8:
                    newTime.setTimeAction("待服务");
                    newTime.setTimeActionid(24L);
                    process = "护士已接单";
                    break;
                case 9:
                    newTime.setTimeAction("已出发");
                    newTime.setTimeActionid(9L);
                    process = "护士已出发";
                    break;
                case 10:
                    newTime.setTimeAction("服务中");
                    newTime.setTimeActionid(10L);
                    process = "护士已到达";
                    break;
                case 11:
                    newTime.setTimeAction("服务完成");
                    newTime.setTimeActionid(33L);
                    process = "服务完成";
                    break;
                case 12:
                    newTime.setTimeAction("费用核定");
                    newTime.setTimeActionid(30L);
                    break;
                case 13:
                    newTime.setTimeAction("用户确认");
                    newTime.setTimeActionid(35L);
                    break;
                case 14:
                    newTime.setTimeAction("最终支付");
                    newTime.setTimeActionid(36L);
                    process = "最终结算完成";
                    break;
                case 15:
                case 16:
                case 17:
                    newTime.setTimeAction("已完成");
                    newTime.setTimeActionid(37L);
                    process = "订单完成";
                    break;
            }

            if (timeMapper.insertServiceTime(newTime) == 1){
                if(orders.getOrderServiceflag() == 14){
                    newTime.setTimeAction("已完成");
                    newTime.setTimeActionid(37L);
                    timeMapper.insertServiceTime(newTime);
                }
                //return true;
            }
            if (process.equals("noNotify")){
                return true;
            }
            ServiceUsers users = usersMapper.selectServiceUsersByUsersId(orders.getOrderUserid());
            ServiceMsginfo msginfo = msginfoMapper.selectServiceMsginfoByMsg("订单状态");
            List<ServiceUsermsg> usermsgList = usermsgMapper.selectServiceUsermsgByUserId(orders.getOrderUserid());
            if (usermsgList == null) return true;
            Boolean notifyflag = false;
            for (ServiceUsermsg usermsg : usermsgList){
                if (usermsg.getUsermsgMsginfoid().equals(msginfo.getMsginfoId())){
                    if ("0".equals(usermsg.getUsermsgUseFlag())){
                        notifyflag = true;
                        break;
                    }else{
                        return true;
                    }
                }
            }
            if (!notifyflag) return true;

            List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(orderId);
            String retDesc = "";
            for (ServiceOrderscombo orderscombo : orderscomboList){
                ServiceCombo combo = comboMapper.selectServiceComboByComboId(orderscombo.getOrderscomboComboid());
                if (combo == null) continue;
                retDesc += combo.getComboName() + "(" + orderscombo.getOrderscomboCount() + "份);";
                break;
            }
            retDesc += "...";
            ServiceAppinfo appinfo = appinfoService.selectServiceAppinfoByAppinfoId(4L);

            // 获取token
            String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appinfo.getAppinfoAppid(), appinfo.getAppinfoSecret());
            com.alibaba.fastjson2.JSONObject tokenInfo = JSON.parseObject(HttpUtil.get(token_url));
            String accessToken = tokenInfo.getString("access_token");
            // 使用前端code获取手机号码 参数为json格式
            //String url = "https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send?access_token=" + tokenInfo.getString("access_token");
            String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + tokenInfo.getString("access_token");
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("template_id", msginfo.getMsginfoValue());
            paramMap.put("page", "/pages/patOrder/patOrderDetail?orderId="+orderId);
            paramMap.put("touser", users.getUsersOpenid());

            Map<String, Object> data = new HashMap<>();
            Map<String, Object> thing5 = new HashMap<>();
            thing5.put("value", process);
            Map<String, Object> thing1 = new HashMap<>();
            thing1.put("value", retDesc);
            data.put("thing5", thing5);
            data.put("thing1", thing1);
            paramMap.put("data", data);
            paramMap.put("miniprogram_state", "developer");
            paramMap.put("lang", "zh_CN");
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
            System.out.println(httpEntity);
            ResponseEntity<Object> response = restTemplateHospital.postForEntity(url, httpEntity, Object.class);

            String body = String.valueOf(response.getBody());// 解析JSON字符串


        }catch (Exception e){
            return false;
        }
        return false;
    }

    @Transactional//定位打卡拍照
    public Boolean saveLocPic(Long orderId, String pic, String loc, String location, LoginHospital oldHospital)
    {
        try {
            ServiceClockin clockin = new ServiceClockin();
            clockin.setClockinOrder(orderId);
            clockin.setClockinNurse(oldHospital.getUserId());

            List<ServiceClockin> oldClock = clockinMapper.selectServiceClockinList(clockin);
            if (oldClock.size() > 0){
                clockin.setClockinId(oldClock.get(0).getClockinId());
            }

            clockin.setClockinPic(pic);
            clockin.setClockinLoc(loc);
            clockin.setClockinLocation(location);
            clockin.setClockinCreateBy(oldHospital.getUsername());
            clockin.setClockinCreateTime(new Date());
            clockin.setClockinUpdateBy(oldHospital.getUsername());
            clockin.setClockinUpdateTime(new Date());

            if (clockin.getClockinId() == null){
                clockinMapper.insertServiceClockin(clockin);
            }else{
                clockinMapper.updateServiceClockin(clockin);
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }


    @Transactional//得到月安排
    public List<HashMap<String, Object>> getMonthSchedule(String year, String month, LoginHospital oldHospital)
    {
        try {
            List<HashMap<String, Object>> result = new ArrayList<>();

            YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month));
            // 获取该月的第一天
            LocalDate firstDayOfMonth = yearMonth.atDay(1);
            // 获取该月的第一天的第一个小时
            LocalDateTime firstHourOfMonth = firstDayOfMonth.atStartOfDay();
            // 获取该月的最后一天
            LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
            // 获取该月的最后一天的最后一个小时
            LocalDateTime lastHourOfMonth = lastDayOfMonth.atTime(LocalTime.MAX);

            ServiceOrders orders = new ServiceOrders();

            orders.setOrderUseFlag("0");
            orders.setOrderDelFlag("0");

            // 获取 firstDayOfMonth 的下一天
            LocalDate nextDayOfMonth = firstDayOfMonth.plusDays(1);
            LocalDateTime nextDayFirstHour = nextDayOfMonth.atStartOfDay();

            for (nextDayFirstHour = firstHourOfMonth; nextDayFirstHour.isBefore(lastHourOfMonth); nextDayFirstHour = nextDayFirstHour.plusDays(1)){
                //HashMap<String, Object> map = new HashMap<>();
                //map.put("beginOrderScheduledtime", nextDayFirstHour);
                //map.put("endOrderScheduledtime", nextDayFirstHour.plusDays(1));

                Date beginOrderScheduledtime = Date.from(nextDayFirstHour.atZone(ZoneId.systemDefault()).toInstant());
                orders.setOrderCreatetime(beginOrderScheduledtime);
                Date endOrderScheduledtime = Date.from(nextDayFirstHour.plusDays(1).atZone(ZoneId.systemDefault()).toInstant());
                orders.setOrderUpdateTime(endOrderScheduledtime);

                List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByNurseId(oldHospital.getUserId());
                Long count = 0L;
                HashMap<String, Object> retNode = new HashMap<>();
                for (ServiceOrdernurse ordernurse : ordernurseList){
                    orders.setOrderId(ordernurse.getOrdernurseOrderid());
                    List<ServiceOrders> order = ordersMapper.selectServiceOrdersListSchedule(orders);
                    if (order.size() > 0){
                        for (ServiceOrders node : order) {
                            if (node.getOrderScheduledtime().after(beginOrderScheduledtime) && node.getOrderScheduledtime().before(endOrderScheduledtime)) {
                                count++;
                            }
                        }

                    }
                }
                if (count > 0){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String formattedDate = sdf.format(beginOrderScheduledtime);
                    retNode.put("date", formattedDate);
                    retNode.put("scheduleCount", count);
                    result.add(retNode);
                }

            }

            return result;
        }catch (Exception e){
            return null;
        }
    }

    @Transactional//得到某日安排
    public List<ScheduleVO> getScheduleList(String date, LoginHospital oldHospital)
    {
        try {
            List<ScheduleVO> result = new ArrayList<>();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(date, formatter);

            LocalDateTime startOfDay = dateTime.toLocalDate().atStartOfDay();;
            LocalDateTime endOfDay = dateTime.toLocalDate().atTime(LocalTime.MAX);

            ServiceOrders orders = new ServiceOrders();

            orders.setOrderUseFlag("0");
            orders.setOrderDelFlag("0");

            Date beginOrderScheduledtime = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            orders.setOrderCreatetime(beginOrderScheduledtime);
            Date endOrderScheduledtime = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
            orders.setOrderUpdateTime(endOrderScheduledtime);

            List<ServiceOrdernurse> ordernurseList = ordernurseMapper.selectServiceOrdernurseListByNurseId(oldHospital.getUserId());
            for (ServiceOrdernurse ordernurse : ordernurseList){
                orders.setOrderId(ordernurse.getOrdernurseOrderid());
                List<ServiceOrders> order = ordersMapper.selectServiceOrdersListSchedule(orders);
                ScheduleVO retNode = new ScheduleVO();
                if (order.size() > 0){
                    for (ServiceOrders node : order) {
                        if (node.getOrderScheduledtime().after(beginOrderScheduledtime) && node.getOrderScheduledtime().before(endOrderScheduledtime)) {

                            ServiceServed served = servedMapper.selectServiceServedByServedId(node.getOrderServedid());
                            ServiceAddress address = addressMapper.selectServiceAddressByAddressId(served.getServedAddress());
                            retNode.setOrderId(node.getOrderId());

                            retNode.setServePerson(served.getServedName());
                            retNode.setServeTel(served.getServedTelephonenumber());
                            retNode.setServeAddress(address.getAddressInfo());

                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String formattedDate = sdf.format(node.getOrderScheduledtime());
                            retNode.setServeTime(formattedDate);

                            List<ItemScheduleInfo> itemScheduleInfoList = new ArrayList<>();
                            List<ServiceOrderscombo> orderscomboList = orderscomboMapper.selectServiceOrderscomboByOrderId(node.getOrderId());
                            for (ServiceOrderscombo orderscombo : orderscomboList) {
                                ServiceItemscombo itemscombo = itemscomboMapper.selectByComboId(orderscombo.getOrderscomboComboid());
                                ServiceItems item = itemsMapper.selectServiceItemsByItemId(itemscombo.getItemscomboItemid());
                                if (item != null && item.getItemId() != 0L){
                                    ServiceCombo combo = comboMapper.selectServiceComboByComboIdNew(orderscombo.getOrderscomboComboid());
                                    ItemScheduleInfo itemScheduleInfo = new ItemScheduleInfo();
                                    itemScheduleInfo.setName(item.getItemName());
                                    itemScheduleInfo.setId(combo.getComboId());
                                    itemScheduleInfo.setComboName(combo.getComboName());

                                    itemScheduleInfoList.add(itemScheduleInfo);
                                }
                            }

                            retNode.setServiceInfo(itemScheduleInfoList);

                            switch (node.getOrderServiceflag()) {
                                case 8:
                                    retNode.setCode("unserved");
                                    retNode.setStatus("待服务");
                                    break;
                                case 9:
                                case 10:
                                    retNode.setCode("serving");
                                    retNode.setStatus("服务中");
                                    break;
                                case 11:
                                case 12:
                                case 13:
                                    retNode.setCode("unsettled");
                                    retNode.setStatus("结算中");
                                    break;
                                case 14:
                                case 15:
                                case 16:
                                case 17:
                                    retNode.setCode("settled");
                                    retNode.setStatus("已完成");
                                    break;
                            }

                        }
                    }
                }
                if (retNode.getOrderId() != null){
                    result.add(retNode);
                }
            }
            return result;
        }catch (Exception e){
            return null;
        }
    }




}
